package com.vcredit.creditcat.web.app;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.IContactManager;
import com.vcredit.creditcat.api.IEmergencyContactManager;
import com.vcredit.creditcat.api.IMobileCallRecordCollectionManaager;
import com.vcredit.creditcat.dto.Linkman;
import com.vcredit.creditcat.dto.LinkmansSource;
import com.vcredit.creditcat.dto.SaveContactsDto;
import com.vcredit.creditcat.exception.CustLoginException;
import com.vcredit.creditcat.model.CallLog;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.util.AppUtil;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.creditcat.vo.CallLogVo;
import com.vcredit.creditcat.vo.CallLogVoList;
import com.vcredit.creditcat.vo.JsonResult;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.exception.ParamException;
import com.vcredit.framework.util.EmojiUtil;

/**
 * 添加通讯录
 * 
 * @author zhangboshi
 *
 */
@RestController
@RequestMapping("/contacts")
public class ContactsController extends BaseCreditCatController {

	@Reference(version = "1.0")
	private IContactManager contactManager;

	@Reference(version = "1.0")
	private IEmergencyContactManager emergencyContactManager;
	@Reference(version = "1.0")
	private IMobileCallRecordCollectionManaager mobileProcessManager;

	/**
	 * 批量添加通讯录联系人信息
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Log
	@RequestMapping(value = "/saveAddressList")
	public JsonResult<SaveContactsDto> saveLinkmans(@RequestBody LinkmansSource linkmansSource,
			HttpServletRequest request) throws Exception {
		Cust currentCust = getCust(request);
		if (null == currentCust) {
			throw new CustLoginException("无登录用户");
		}
		if (null == linkmansSource || null == linkmansSource.getLinkmans() || linkmansSource.getLinkmans().isEmpty()) {
			throw new ParamException("联系人信息为空");
		}
		Boolean operationResult = contactManager.saveAddressLists(currentCust.getCustId(), linkmansSource);
		linkmansSource = null;
		SaveContactsDto reponse = new SaveContactsDto();
		reponse.setOperationResult(operationResult);
		reponse.setDisplayInfo((operationResult) ? "添加通讯录成功" : "添加通讯录失败");
		return JsonResult.SuccessResponse(reponse);
	}

	/**
	 * 批量添加紧急联系人信息
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	@Log
	@RequestMapping(value = "/saveLinkmans")
	public JsonResult saveEmergencyContacts(@RequestBody LinkmansSource linkmansSource, HttpServletRequest request)
			throws Exception {
		Cust currentCust = getCust(request);
		if (null == currentCust) {
			throw new CustLoginException("无登录用户");
		}
		if (null == linkmansSource || null == linkmansSource.getLinkmans() || linkmansSource.getLinkmans().isEmpty()) {
			throw new ParamException("联系人信息为空");
		}
		for(Linkman linkman :linkmansSource.getLinkmans()){
			Utility.formatPhoneNo(EmojiUtil.filterEmoji(linkman.getMobileNo()), true);			
		}
		emergencyContactManager.saveEmergencyContacts(currentCust, linkmansSource);
		return JsonResult.SuccessResponse(null);
	}

	/**
	 * 封装参数
	 * 
	 * @param callLogDto
	 * @param cust
	 * @return
	 */

	@Log
	@RequestMapping(value = "/saveCallRecord")
	@SuppressWarnings("rawtypes")
	public JsonResult saveCallRecord(@RequestBody @Valid CallLogVoList callLog, HttpServletRequest request)
			throws Exception {
		Cust cust = getCust(request);
		CallLog log = new CallLog();
		log.setCallerNum(cust.getCustMobile());
		log.setCustId(cust.getCustId());
		List<CallLog> findAllCallRecord = mobileProcessManager.findAllCallRecord(log);

		mobileProcessManager.saveCallRecord(this.convertObject(findAllCallRecord, callLog, cust));
		return JsonResult.SuccessResponse(null);
	}

	/**
	 * 封装参数
	 * 
	 * @param callLogDto
	 * @param cust
	 * @return
	 */
	public List<CallLog> convertObject(List<CallLog> findAllCallRecord, CallLogVoList callLogDto, Cust cust)
			throws Exception {
		CallLog callLog = new CallLog();
		List<CallLog> calllogs = new ArrayList<CallLog>();
		StringBuilder sbu = new StringBuilder();
		// 前端传入
		for (CallLog callLogadd : findAllCallRecord) {
			sbu.append(callLogadd.getCallTime());
		}
		List<CallLogVo> listVo = callLogDto.getCallLog();

		for (CallLogVo callLogDtos : listVo) {
			// 查询是否之前是否采集过 ，采集过则不用插入
			if (!sbu.toString().contains(this.calog(callLog, callLogDtos, cust).getCallTime() + "")) {
				callLog = new CallLog();
				calllogs.add(this.calog(callLog, callLogDtos, cust));
			}
		}
		return calllogs;
	}

	public CallLog calog(CallLog callLog, CallLogVo callLogDtos, Cust cust) {
		/** 通话人电话号码 **/
		callLog.setCallerNum(callLogDtos.getMobileNo());
		/** 通话时间 **/
		callLog.setCallTime(AppUtil.stampToDate(callLogDtos.getCallTime() + ""));
		/** 通话时长 **/
		callLog.setTalkTime(callLogDtos.getDurationSec() + "");
		/** 1、来电 2、拨出3、未接 **/
		callLog.setCallType(callLogDtos.getCallDirection());
		/** 用户id **/
		callLog.setCustId(cust.getCustId());
		return callLog;
	}
}
